<template>
  <div class="app-container dialog-w80">
    <div class="customContainer">
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item>
          <el-date-picker
            v-model="pickerDate"
            value-format="yyyy.MM.dd"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-select filterable placeholder="仓库" v-model="formInline.house_id">
            <el-option
              v-for="item in house_list"
              :key="item.house_id"
              :label="item.name"
              :value="item.house_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select placeholder="选择类型" v-model="formInline.species" filterable>
            <el-option v-for="item in type_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select placeholder="订单状态:" v-model="formInline.status">
            <el-option v-for="item in status" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="formInline.order_no"
            placeholder="请输入采购单号"
            @keyup.enter.native="searchClick"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick">
            <template #default>搜索</template>
          </dj-button>
        </el-form-item>
      </el-form>

      <!-- 列表 -->
      <el-table highlight-current-row :data="list" border size="small">
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="scope">
            <span class="indexColor">{{scope.$index+1+(currentPage-1)*20}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="订单状态">
          <template slot-scope="scope">
            <span v-if="scope.row.status==0">待审核</span>
            <span v-if="scope.row.status==1">待确认</span>
            <span v-if="scope.row.status==2">待发货</span>
            <span v-if="scope.row.status==3">部分收货</span>
            <span v-if="scope.row.status==4">订单完结</span>
            <span v-if="scope.row.status==5">收货完成</span>
            <span v-if="scope.row.status==6">订单取消</span>
            <span v-if="scope.row.status==8">待收货</span>
            <span v-if="scope.row.status==9">申请退货中</span>
            <span v-if="scope.row.status==10">退货中</span>
            <span v-if="scope.row.status==11">退货完成</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="订单编号">
          <template slot-scope="scope">
            <el-button
              @click="get_order_info(scope.row.id,scope.$index,scope.row.status,scope.row.name)"
              type="text"
              size="small"
            >{{scope.row.order_id}}</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" label="下单仓" prop="name"></el-table-column>
        <el-table-column align="center" label="下单时间" prop="creat_time"></el-table-column>
        <el-table-column align="center" label="备注">
          <template slot-scope="scope">
            <span class="indexColor">{{!scope.row.remark?'---':scope.row.remark}}</span>
          </template>
        </el-table-column>
        <!-- 列表的操作按钮 -->
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status==1||scope.row.status==2||scope.row.status==3||scope.row.status==8"
              @click="closeClick(scope.row.id,scope.$index,0)"
              type="danger"
              size="mini"
            >关闭订单</el-button>
            <el-button v-else type="info" disabled size="mini">不可操作</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹框 -->
    <el-dialog
      :close-on-click-modal="false"
      class="dialog_table"
      title="物品单确认"
      :visible.sync="detail_dialog"
    >
      <div class="dialogInContainer" v-loading="dialogLoading">
        <div style="position:relative;top:-20px">下单仓：{{name}}</div>
        <el-table highlight-current-row :data="infolist" border size="small">
          <el-table-column align="center" label="图片" width="70">
            <template slot-scope="scope">
              <img :src="scope.row.thumb" width="50" height="50" alt />
            </template>
          </el-table-column>
          <el-table-column align="center" label="物品编号" prop="accessories_id"></el-table-column>
          <el-table-column align="center" label="神采编号">
            <template slot-scope="scope">{{scope.row.no_obsolete}}</template>
          </el-table-column>
          <el-table-column align="center" label="物品名称" prop="accessories_name"></el-table-column>
          <el-table-column align="center" label="类别" prop="type_name"></el-table-column>
          <el-table-column align="center" label="采购价" prop="money_sell"></el-table-column>
          <el-table-column align="center" label="订单数">
            <template slot-scope="scope">
              <el-input
                onkeypress="return event.which>=48&&event.which<=57"
                type="number"
                :disabled="isDisabled"
                onmousewheel="return false;"
                v-model="scope.row.total_num"
                placeholder="数量"
                @input="count"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" label="采购总价">
            <template slot-scope="scope">{{scope.row.total_num*1*scope.row.money_sell*1|keepTwoNum}}</template>
          </el-table-column>
          <el-table-column align="center" label="可采购数" prop="house_num"></el-table-column>
          <el-table-column align="center" label="备注">
            <template slot-scope="scope">
              <span class="danger" v-if="scope.row.is_insufficient==1">礼品库存不足</span>
              <span class="danger" v-if="scope.row.is_insufficient==2">礼品已下架</span>
              {{scope.row.remark_p}}
            </template>
          </el-table-column>
          <!-- 列表的操作按钮 -->
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-switch
                :disabled="isDisabled"
                active-value="0"
                inactive-value="1"
                v-model="scope.row.is_del"
                @change="count"
              ></el-switch>
            </template>
          </el-table-column>
        </el-table>
        <!-- 自定义底部 -->
        <br />
        <div class="bottom_box">
          <el-button
            class="pull-right"
            :disabled="isDisabled"
            size="small"
            type="primary"
            @click="saveClick()"
          >确认提交</el-button>
          <p
            class="pull-right"
            style="line-height:0;margin-right:10px"
          >物品总数量:{{totalOrder}} 物品总金额:{{totalPrice|keepTwoNum}}</p>
        </div>
      </div>
    </el-dialog>
    <!-- 自定义底部 -->
    <div class="bottom_box">
      <el-pagination
        class="pull-left"
        background
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="20"
        layout="prev, pager, next, jumper"
        :total="totalDataNumber"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import {
  house,
  getList,
  getPage,
  order_info,
  save,
  close,
  get_goods_type,
} from "@/api/goodsflow/pj_confirm";
import { pickerOptionsF, pickerDateF, optionMaxTime } from "@/utils";
export default {
  data() {
    return {
      dialogLoading: false,
      listLoading: false,
      type_list: null,
      name: "",
      status: [
        { id: -1, name: "全部" },
        { id: 1, name: "待确认" },
        { id: 2, name: "待发货" },
        { id: 3, name: "部分收货" },
        { id: 4, name: "订单完结" },
        { id: 5, name: "收货完成" },
        { id: 6, name: "订单取消" },
        { id: 8, name: "待收货" },
        { id: 9, name: "申请退货中" },
        { id: 10, name: "退货中" },
        { id: 11, name: "退货完成" },
      ],
      formInline: {
        species: "",
        house_id: 0,
        order_no: "",
        start: "",
        end: "",
        status: 1,
        position: "verify",
      },
      pickerOptions: pickerOptionsF(),
      pickerDate: pickerDateF(),
      house_list: [],
      list: [],
      totalDataNumber: 1,
      currentPage: 1,
      detail_dialog: false,
      infolist: [],
      totalOrder: "0",
      totalPrice: "0",
      saveForm: {
        order_id: "",
        is_insufficient: "1",
        order_list: [],
      },
      editIndex: "",
      isDisabled: false,
      isShow: false,
    };
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      //当进入组件之前，执行 beforRouteEnter 路由钩子函数 ,//参数vm就是当前组件的实例。
      if (!vm.type_list) {
        vm.getGoods();
        vm.gethouse();
      }
    });
  },
  // created() {
  //     this.gethouse();
  //     this.getGoods();
  // },
  methods: {
    getGoods() {
      get_goods_type({ category_id: this.$route.meta.id }).then((response) => {
        response.forEach((item) => {
          if (item.name == "配件类物品") {
            item.id = 1;
          } else if (item.name == "财务类物品") {
            item.id = 2;
          } else if (item.name == "其他类物品") {
            item.id = 3;
          }
        });
        this.type_list = response;
        this.formInline.species = response.length > 0 ? response[0].id : "";
      });
    },
    gethouse() {
      house().then((response) => {
        this.house_list = response;
      });
    },
    searchClick() {
      optionMaxTime(this.pickerDate).then(() => {
        //最大可选择时间区间
        this.formInline.start = this.pickerDate ? this.pickerDate[0] : "";
        this.formInline.end = this.pickerDate ? this.pickerDate[1] : "";
        this.fetchData();
      });
    },
    // 关闭订单
    closeClick(id, index, status) {
      this.editIndex = index;
      this.$confirm("此操作为关闭, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        close({ order_id: id, status: status }).then((response) => {
          this.$message({
            type: response.code == 1 ? "success" : "error",
            message: response.message,
          });
          if (response.code == 1) {
            this.list[this.editIndex].status = 6;
          }
        });
      });
    },
    // 提交
    saveClick() {
      const that = this;
      that.saveForm.order_list = [];
      this.infolist.forEach(function (val, index) {
        if (val.order_num == "" || val.order_num == 0) {
          return;
        } else {
          that.saveForm.order_list.push({
            look_money: val.money_sell,
            house_id: val.house_id,
            accessories_id: val.accessories_id,
            total_num: val.total_num,
            is_del: val.is_del,
            type: val.type,
            remark: val.remark ? val.remark : "",
            mapping_id: val.id,
            money_sell: val.money_sell,
            hosue_id: val.hosue_id,
          });
        }
      });
      if (this.saveForm.order_list.length == this.infolist.length) {
        this.$confirm("此操作提交, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          save(this.saveForm).then((response) => {
            this.$message({
              type: response.code == 1 ? "success" : "error",
              message: response.message,
            });
            if (response.code == 1) {
              this.list[this.editIndex].status = 1;
              this.detail_dialog = false;
              this.searchClick();
            }
          });
        });
      } else {
        that.$message({ type: "error", message: "请将所有信息填写完整!" });
        return;
      }
    },
    // 打开订单详情
    get_order_info(id, index, status, name) {
      this.dialogLoading = true;
      this.name = name;
      this.isDisabled = status == 1 ? false : true;
      this.editIndex = index;
      this.saveForm.order_id = id;
      this.detail_dialog = true;
      order_info({ order_id: id }).then((response) => {
        this.infolist = response;
        this.count();
        this.dialogLoading = false;
      });
    },
    // 计算
    count() {
      var totalPrice = 0; //临时总价
      var totalOrder = 0;
      this.infolist.forEach((val, index) => {
        if (val.is_del * 1 == 0) {
          totalPrice += val.total_num * 1 * val.money_sell * 1; //累计总价
          totalOrder += val.total_num * 1;
        }
      });
      this.totalPrice = parseFloat(totalPrice);
      this.totalOrder = parseFloat(totalOrder);
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      getPage(this.formInline)
        .then((response) => {
          this.totalDataNumber = response.page * 20;
          getList(this.formInline).then((response) => {
            this.list = response;
            this.listLoading = false;
          });
        })
        .catch((res) => {
          this.listLoading = false;
        });
    },
    handleCurrentChange(val) {
      this.listLoading = true;
      getList(this.formInline, val).then((response) => {
        this.list = response;
        this.listLoading = false;
      });
    },
  },
};
</script>